import React ,{useState,useRef,useEffect}from "react";
import { PageContainer } from "@ant-design/pro-layout";
import ProTable from "@ant-design/pro-table";
import { Button,message,Modal } from "antd";
import { queryChannel,addChannel,updateChannel,removeChannel,queryApi } from "@/services/ant-design-pro/apis";
import CreateChannel from "./component/CreateChannel";
import UpdateChannel from "./component/UpdateChannel";
const BizType = (props) => {
     
    const actionRef = useRef();
    const [selRows,setSelRows]=useState();
    const [createModalVisible,handleModalVisible]=useState(false);
    const [updateModalVisible,handleUpdteModalVisible]=useState(false);
    const [stepFormVals,setStepFormVals]= useState();
    const [exportModalVisible, handleExportModalVisible] = useState(false);
    const param={};
    const [apis, setApis] = useState([]);


    useEffect(() => {
        queryApi().then((val)=>{

            setApis(val.data)
        })
       
    }, []);


    const handleDel = async (record) => {
        Modal.confirm({
            title: '删除渠道',
            content: '确认删除渠道？',
            okText: '确认',
            cancelText: '取消',
            onOk: async () => {
                const hide = message.loading('正在删除');
               try{
                await removeChannel({ id: record.id })
                hide();
                message.success('删除成功');
                actionRef.current.reload();
               }catch(error){
                hide();
                message.error('删除失败请重试！');
                return false;
               }
            }
        })
    }

    const handleAdd = async (value) =>{
        const hide = message.loading("正在添加");
        try{
            await addChannel({...value})
            hide();
            handleModalVisible(false);
            message.success("添加成功");
            actionRef.current.reload();
            return true;
        }catch(error){
            hide();
            message.error("添加失败")
            return false;
        }
    }

    const handleUpdate = async (value) =>{
        const hide = message.loading("正在配置");
        try{
            await updateChannel({...value})
            hide();
            handleUpdteModalVisible(false);
            setStepFormVals(undefined);
            message.success("配置成功");
            actionRef.current.reload();
        }catch(error){
            hide();
            message.error("配置失败")
        }
    }



    const columns=[
        {
            title:'id',
            dataIndex:'id',
            hideInSearch:true,
        },
        {
            title:'渠道名称',
            dataIndex:'channelName'
        },
        {
            title:'渠道编号',
            dataIndex:'channelNum'
        },
        {
            title:'余额',
            dataIndex:'balance',
            hideInSearch:true,
            
        },
        {
            title:'折扣',
            dataIndex:'discount',
            hideInSearch:true,
        },
        {
            title:'api名称',
            dataIndex:'apiName',
            hideInSearch:true,
           
        },
        {
            title: '创建时间',
            dataIndex: 'creationTime',
            valueType: 'dateTime',
            hideInSearch:true,
        },
        {
            title: '修改时间',
            dataIndex: 'modificationTime',
            valueType: 'dateTime',
            hideInSearch:true,
        },
        {
            title: '渠道状态',
            dataIndex: 'status',
            valueEnum: {
                "INVOKE": {
                    text: '正常',
                    status: 'Success'
                },
                'FORBIDDEN': {
                    text: '禁用',
                    status: 'Error'
                },
            }
        },
        {
            title:'操作',
            dataIndex:'option',
            valueType:'option',
            render:(_,record)=>[
                <a key="edit" onClick={()=>{
                    setStepFormVals(record);
                    handleUpdteModalVisible(true);
                }}>配置</a>,
                <a key="delete" onClick={()=>{
                    handleDel(record);
                }}>删除</a>,
            ]
        }
    ]

    return (
        <PageContainer>
            <ProTable
               columns={columns}
               actionRef={actionRef}
               params={param}
               headerTitle='渠道列表'
               rowKey='id'
               request={queryChannel}
               rowSelection={{
                   onChange:(_,selRows)=>{
                       setSelRows(selRows)
                   }
               }}
               toolBarRender={()=>[
                <Button type="primary" onClick={()=>{
                    handleModalVisible(true)
                }}>新建</Button>
            ]}
               search={{
                defaultCollapsed: false
              }}
            >   
                       
            </ProTable>

            <CreateChannel
              createModalVisible={createModalVisible}
              handleModalVisible={()=>{handleModalVisible(false)}}
              handCreate={()=>{}}
              onSubmit={async (values) => {
                await handleAdd(values)
              }}
              apis={apis}
              onCancel={()=>{
                handleModalVisible(false)
              }}
            >
                {/* <ProTable
                  type="form"
                  columns={columns}
                  form={{initialValues:{productStatus:"99",productType:"1",receiveType:"1"}}}
                  onSubmit={async (values)=>{
                       await handleAdd(values)
                  }}
                >
                </ProTable> */}
            </CreateChannel>

            {stepFormVals && Object.keys(stepFormVals).length?(
                <UpdateChannel
                  updateModalVisible={updateModalVisible}
                  handleModalVisible={()=>{
                    handleUpdteModalVisible(false)
                    setStepFormVals(undefined)
                }}
                  onCanel={()=>{
                      handleUpdteModalVisible(false)
                      setStepFormVals(undefined);
                  }}
                  onSubmit={(values)=>{
                    handleUpdate(values)
                  }}
                  values={stepFormVals}
                  apis={apis}
                >

                </UpdateChannel>
            ):null}

        </PageContainer>
    )
}

export default  BizType;

